<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

if(!defined('TinyFW')) exit('Access denied!');

class tfRouter {
    
    private static $instance;

    public function __clone(){}

    private function __construct() {

    }


    public static function singleton() {
        if( ! isset(self::$instance)  || ! is_object(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function _get_action_file() {
        
        //echo APP_PATH . Uri::singleton()->getAppName() . DS . "modules". DS . Uri::singleton()->getModuleName() . DS . "actions".DS . Uri::singleton()->getActionName() . ".class.php";
        //如果存在單獨的action文件，載入單獨的action文件，否則的話，直接載入actions.class.php
        if(file_exists(APP_PATH . tfUri::getAppName() . DS . "modules".DS . tfUri::getModuleName() . DS ."actions".DS . tfUri::getActionName() . ".class.php")) {

            $action_file = APP_PATH . tfUri::getAppName() . DS."modules".DS . tfUri::getModuleName() .DS. "actions".DS . tfUri::getActionName() . ".class.php";

        } elseif(file_exists(APP_PATH . tfUri::getAppName() . DS."modules".DS . tfUri::getModuleName() . DS ."actions".DS ."actions.class.php")) {

            $action_file = APP_PATH . tfUri::getAppName() .DS. "modules".DS . tfUri::getModuleName() . DS ."actions".DS."actions.class.php";

        } else {
            trigger_error("Cannot find the action file", E_USER_ERROR);
        }

        //echo $action_file;
        return $action_file;
    }

    
   public static function dispatch() {
       //
       //echo tfUri::getAppName()."<br>";
       //echo tfUri::getModuleName()."<br>";
       //echo tfUri::getActionName()."<br>";
       //調用相關的action來處理請求

       include_once(self::singleton()->_get_action_file());
       if(class_exists("Action")) {
           //echo "class exists";
           $action = new Action();
       }

       $action->run();
       
       //$action->indexAction();
       

   }

}


?>
